package com.fiill.audioplayernativelib;
import android.util.Log;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import android.util.Log;

public class WaveParseLib {
    private static final String TAG = "BAHAL_WaveParseLib";
    private String mFileUrl;
    private WaveHeader mWaveHeader; 

    // Used to load the 'audioplayernativelib' library on application startup.
    /*
    static {
        System.loadLibrary("audioplayernativelib");
    }

    public native String getParseString(String fileUrl);

    public WaveParseLib(String fileUrl) {
        mFileUrl = fileUrl;
        String parseStr = getParseString(mFileUrl);
        mWaveHeader = new WaveHeader(parseStr);
    }*/

    public WaveParseLib(String fileUrl) {
        mFileUrl = fileUrl;
        mWaveHeader = parseWavHeader(mFileUrl);
        Log.i(TAG, "parseWavHeader " + mWaveHeader.toString());
    }

    private WaveHeader parseWavHeader(String fileUrl) {
        WaveHeader header = new WaveHeader();
        try {
            FileInputStream inputStream = new FileInputStream(fileUrl);
            byte[] headerData = new byte[44];
            if (inputStream.read(headerData) != 44) {
                return header;
            }
            header.channels = ByteBuffer.wrap(sliceArray(headerData, 22, 23)).order(ByteOrder.LITTLE_ENDIAN).getShort();
            header.sample_rate = ByteBuffer.wrap(sliceArray(headerData, 24, 27)).order(ByteOrder.LITTLE_ENDIAN).getInt();
            header.bit_format = ByteBuffer.wrap(sliceArray(headerData, 34, 35)).order(ByteOrder.LITTLE_ENDIAN).getShort();
            header.data_size = ByteBuffer.wrap(sliceArray(headerData, 40, 43)).order(ByteOrder.LITTLE_ENDIAN).getInt();
            header.offset = 44;
            inputStream.close();
         } catch (IOException e) {
            ;
         }
         return header;
    }

    private static byte[] sliceArray(byte[] array, int start, int end) {
        int length = end - start + 1;
        byte[] result = new byte[length];
        System.arraycopy(array, start, result, 0, length);
        return result;
    }

    class WaveHeader {
        public  int channels;
        public  int offset;
        public  int sample_rate;
        public  int bit_format;
        public  int data_size;

        WaveHeader(String parseStr) {
            stringSplite(parseStr);
        }

        WaveHeader() {}

        public String toString() {
            return "ch:" + channels +
                   " sample_rate:" + sample_rate +
                   " bit_format:" + bit_format +
                   " data_size:" + data_size;
        }

        void stringSplite(String input) {
            String[] lines = input.split("\n");
            for (String line : lines) {
              String[] parts = line.split(":"); 
              if (parts.length == 2) {
                try {
                    int value = Integer.parseInt(parts[1].trim());
                    if (parts[0].equals("offset")) offset = value;
                    if (parts[0].equals("channels")) channels = value;
                    if (parts[0].equals("sample_rate")) sample_rate = value;
                    if (parts[0].equals("bit_format")) bit_format = value;
                    if (parts[0].equals("data_size")) data_size = value;
                } catch (NumberFormatException e) {
                    Log.e(TAG, "Invalid value: " + parts[1]);
                }
              }
            }
        }
    }

    public  int getOffset() {
        return mWaveHeader.offset;
    }

    public  int getChannels() {
        return mWaveHeader.channels;
    }

    public  int getSampleRate() {
        return mWaveHeader.sample_rate;
    }

    public  int getBitFormat() {
        return mWaveHeader.bit_format;
    }

    public  int getDataSize() {
        return mWaveHeader.data_size;
    }
}
